<?php
load_info_var($cf_controller);
load_file('file_class', LIB_PATH);
load_file('folder_class',LIB_PATH);
$smarty->mypage($cf_controller, $cf_action, 0);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$rid = intval($_POST['rid']) ? intval($_POST['rid']) : intval($_GET['rid']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
//$contact = new Contact($db);
$main = new File($db);
$folder = new Folder($db);

switch ($cf_action) {
    case "index":
        $smarty->mypage($cf_controller, $cf_action, 0);
		//$rid = '1';
        $data_ary = $folder->get_data_list($rid,$cf_admin_info['admin_id']);
		
		$data_share = $folder->get_data_share($cf_admin_info['admin_id']);
		//my_print($data_share);
        break;
	case "open":
        $smarty->mypage($cf_controller, $cf_action, 0);
		$folder_id = $id;
		$where="file_folder='$id'";
		load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
		//$db->setDebug(1,1);
        $data_ary = $MyPage->from_sql('*', 'icc_file', $where, "file_id DESC", intval($_GET['cpage']), "20");
	    $page = $MyPage->get_page($data, $cf_self_page . '?skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3 . '&stype=' . $stype . '&self=' . $self);
        break;
	case "openshare":
        $smarty->mypage('file_info', $cf_action, 0);
		$folder_uid = $_GET['folder'];
		$folder_id = $id;
		$where="file_folder='$id'";
		load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
		//$db->setDebug(1,1);
        $data_ary = $MyPage->from_sql('*', 'icc_file', $where, "file_id DESC", intval($_GET['cpage']), "20");
	    $page = $MyPage->get_page($data, $cf_self_page . '?skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3 . '&stype=' . $stype . '&self=' . $self);
        break;
	case "share":
        $smarty->mypage('file_info', $cf_action, 0);
		$folder_uid = $_GET['folder'];
		$share = '1';
		//$data_ary = $folder->get_data_share_list($rid,$folder_uid,$share);
		$data_ary = $folder->get_data_list($rid,$folder_uid);
		//my_print($data_ary);
		break;
    case "add":
        if ($cf_ispost) {
            $data = array(
				'file_folder' =>intval($_POST['file_folder']),
                'file_title' => trim($_POST['file_title']),
				'file_type' => trim($_POST['file_type']),
                'file_introduction' => trim($_POST['file_introduction']),
                'file_remark' => trim($_POST['file_remark']),
                'file_content' => trim($_POST['act_file']),  
				'file_uid' => $cf_admin_info['admin_id'],      
                'file_creator' => $cf_admin_info['admin_name'],
                'file_create_time' => date("y-m-d H:i:s"),
            );
            //$db->setDebug(1,1);
            //檢查相同用戶
           
            $result = $main->insert_data($data);

            if ($result) {
                $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                unset($_SESSION['pre_url']);
                $more = $_SERVER['HTTP_REFERER'] ? '<br /><a class="edit" onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
                my_jump('添加成功' . $more, '', $return_page, 3);
            } else {
                my_jump('添加失败');
            }
        } else {
            $smarty->mypage($cf_controller, $cf_action, 0);
            $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
			$folder_id = $_GET['folder'] ? $_GET['folder'] : '';
			$folder_root = $folder->get_data_root($cf_admin_info['admin_id'],1);
			$folder_list = $folder->get_data_tree($folder_root['folder_id'],1,$cf_admin_info['admin_id']);
			//$folder_list = $folder->get_data_tree(1,1);
			//my_print($folder_list);
			$folder_ary = change_array($folder_list,'folder_id','folder_title1');
        }
        break;
	
	case "upphoto":
		load_file('upload_class',LIB_PATH);
		$config=array(													//参数设置
		'filename'  => array(),												//新文件名,如空为随机
		'maxsize'   => 1024, 												//最大文件大小 k为单位
		'type'      => array("jpg","gif","png","txt","docx","doc","xls","xlsx","zip","rar","ppt","pptx"),							//允许类型
		'path'      => '',											//文件存入目录
		'thumb_path'=> '',											//缩略图目录
		'upload_root'=> UPLOAD_FILE,										//上传根目录
		'datepath'  => true,												//是否用日期分目录
		'date'    	=> ''													//日期目录方式,不可设置
	 	);
		$myupload = new MyUpload($config);
		if($myupload->check_form('Filedata')){
				$output = $myupload->upload('Filedata');
				
				if($output){
					echo implode(",",$output);					
				}else {
					echo "0";
				}
			}
		exit;
	break;
	
	case "delfile":
	   	$main->del_files($_GET['value']);
		echo "1";
		exit;
	break;
		
    case "edit":
        if ($cf_ispost) {
			$folder_id = $_GET['folder'] ? $_GET['folder'] : '';
            $data = array(
				'file_folder' =>intval($_POST['file_folder']),
                'file_title' => trim($_POST['file_title']),
				'file_type' => trim($_POST['file_type']),
                'file_introduction' => trim($_POST['file_introduction']),
                'file_remark' => trim($_POST['file_remark']),
                'file_content' => trim($_POST['act_file']),           
                'file_edit_info' => $cf_admin_info['admin_name'] . ' ' . date("Y-m-d H:i"),
            );
           		 
            $result = $main->update_data($data,"file_id='$id'");

            if ($result) {
                $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                unset($_SESSION['pre_url']);
                my_jump('修改成功', '', $return_page, 1);
            } else {
                my_jump('修改失败');
            }
        } else {
            $smarty->mypage($cf_controller, $cf_action, 0);
			//$folder_id = $_GET['folder'] ? $_GET['folder'] : '';
            $row = $main->get_data_row($id);
            if (!$row)
                my_jump('沒有此信息');
			
            $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
			
			$folder_root = $folder->get_data_root($cf_admin_info['admin_id'],1);
			$folder_list = $folder->get_data_tree($folder_root['folder_id'],1,$cf_admin_info['admin_id']);
			//$folder_list = $folder->get_data_tree(1,1);
			//my_print($folder_list);
			$folder_ary = change_array($folder_list,'folder_id','folder_title1');
        }
		//my_print($row['file_folder']);
        break;
    case "del":
        $id = intval($_GET['id']);
		
		$result = $main->del_data("file_id='$id'");

        if ($result) {
            my_jump('删除成功!', '', $return_page, 1);
        } else {
            my_jump('删除失败1!');
        }

        break;
    case "set":
        $type = $_GET['type'];
        $value = $_GET['value'];
        $where = "ac_id = '" . $id . "'";
        $result = $main->set_data($value, $type, $where);
        if ($result) {

            my_jump('更新完成', '', $return_page, 1);
        } else {
            my_jump('更新失败');
        }
        break;
    case "detail":                                                          //不帶外框,特別處理
        //$smarty->mypage("ac_info", $cf_action, 0);
		$folder_id = $_GET['folder'] ? $_GET['folder'] : '';
        $smarty->mypage($cf_controller, $cf_action, 0);
		$row = $main->get_data_row($id);
		$file_path_arr = explode(",",$row["file_content"]);
		for($i=0;$i<count($file_path_arr);$i++){
			$file_arr = explode("/",$file_path_arr[$i]);
			$file_name[$i][0] = $file_path_arr[$i];
			$file_name[$i][1] = $file_arr[count($file_arr) - 1];
			}
		$smarty->assign("file_name",$file_name);
		//print_r($file_arr); 
        break;
	 case "sharedetail":                                                          //不帶外框,特別處理
        //$smarty->mypage("ac_info", $cf_action, 0);
		$folder_id = $_GET['folder'] ? $_GET['folder'] : '';
		$folder_uid = $_GET['folderuid'];
        $smarty->mypage('file_info', $cf_action, 0);
		$row = $main->get_data_row($id);
		$file_path_arr = explode(",",$row["file_content"]);
		for($i=0;$i<count($file_path_arr);$i++){
			$file_arr = explode("/",$file_path_arr[$i]);
			$file_name[$i][0] = $file_path_arr[$i];
			$file_name[$i][1] = $file_arr[count($file_arr) - 1];
			}
		$smarty->assign("file_name",$file_name);
		//print_r($file_arr); 
        break;
    case "change":
        my_jump('待开发中');
        break;
    case "check":
        $data = array(
            'ac_check' => intval($_POST['ac_check']),
            'ac_reason' => $_POST['ac_check'] == '2' ? intval($_POST['ac_reason']) : '0',
            'ac_check_info' => $cf_admin_info['admin_name'] . ' ' . date("y-m-d H:i:s"));
        $result = $main->update_data($data, "ac_id='$id'");
        if ($result) {
            my_jump('操作完成', '', $return_page, 1);
        } else {
            my_jump('更新失败');
        }
        break;
    case "all":
        $alltype = trim($_POST['alltype']);
        $idlist = $_POST['idlist'];
        $allvalue = trim($_POST['allvalue']);
        if ($idlist) {
            $list = implode(",", $idlist);
            switch ($alltype) {
                case "alldel":
                    $result = $main->del_data("file_id in (" . $list . ")");
                    break;
                case "allcheck":
                    $data = array('ac_check' => '1', 'ac_check_info' => $cf_admin_info['admin_name'] . ' ' . date("y-m-d H:i:s"));
                    $result = $main->update_data($data, "ac_id in (" . $list . ")");
                    break;
                case "alluncheck":
                    $data = array('ac_check' => '0', 'ac_check_info' => $cf_admin_info['admin_name'] . ' ' . date("y-m-d H:i:s"));
                    $result = $main->update_data($data, "ac_id in (" . $list . ")");
                    break;
                case "alloutcheck":
                    $data = array('ac_check' => '2', 'ac_check_info' => $cf_admin_info['admin_name'] . ' ' . date("y-m-d H:i:s"));
                    $result = $main->update_data($data, "ac_id in (" . $list . ")");
                    break;
                default:
                    my_jump('请指定操作方式');
                    break;
            }
            if ($result)
                my_jump('操作成功', '', $return_page, 1);
            else
                my_jump('操作失败');
        }else {
            my_jump('输入的参数有误');
        }
        break;
    default:
        my_jump('非法操作!');
        break;
}

$smarty->assign(array(
    'id' => $id,
	'rid'=>$rid,
    'row' => htmlchar($row),
    'data_ary' => $data_ary,
	'data_share'=>$data_share,
    'page' => $page,
    'sindex' => $sindex,
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
    'stype' => $stype,
    'self' => $self,
	'sessionid'=>session_id(),
	'folder_ary'=>$folder_ary,
	'folder_id'=>$folder_id,
	'folder_uid' => $folder_uid,
	'share'=>$share,
));
$smarty->mydisplay();
?>